# coding=utf-8

import urllib2
import sys
import re
import time

spath = 'result.txt' 
f = open(spath,'w')

total = 0

for i in range(210):
    page = i + 1
    print page
    
    time.sleep(0.5)
    
    content = urllib2.urlopen('http://ikandou.com/book/?page=' + str(page)).read()
    content =  content.decode("UTF-8").encode(sys.getfilesystemencoding())  # 关键
    
#    print content
    
    urls = re.findall('<a href="(.*?)\?next=/book/"><span class="title">.*?</span></a>', content, re.MULTILINE);
    
    for link in urls:
        print link
        f.write(link + '\n')
        total += 1
    
print 'total = ' + str(total)
            